package com.cntcdt.api.common;

import java.io.IOException;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.restlet.data.MediaType;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;

/**
 * 错误信息
 * 
 * @author xiaog
 * 
 */
public class ErrorRepresentation {
    private StringRepresentation stringRepresentation;
    private MediaType mediaType;
    private ErrorCode2 errorCode2;

    public ErrorRepresentation(ErrorCode2 errorCode2, MediaType mediaType) {
	this.errorCode2 = errorCode2;
	this.mediaType = mediaType;

    }

    public Representation getRepresentation() {
    ObjectMapper mapper = new ObjectMapper();
	String s = null;
	try {
	    s = mapper.writeValueAsString(errorCode2);
	} catch (JsonGenerationException e) {
	    e.printStackTrace();
	} catch (JsonMappingException e) {
	    e.printStackTrace();
	} catch (IOException e) {
	    e.printStackTrace();
	}
	if (mediaType.equals(MediaType.APPLICATION_JSON)) {
	    stringRepresentation = new StringRepresentation(s,
		    MediaType.APPLICATION_JSON);
	} else {
	    stringRepresentation = new StringRepresentation(s,
		    MediaType.APPLICATION_XML);
	}

	return stringRepresentation;
    }
}
